jQuery(window).load(function() {
    initFitvids();
    customPostColor(jQuery('article'));
    jQuery(document).on("mouseenter", ".share_box", function(e) {
        jQuery(this).sharrre({
            share: {
                twitter: true,
                facebook: true,
                googlePlus: true,
                linkedin: true,
                pinterest: true
            },
            template: '<div class="box"><div class="middle"><a href="#" class="pinterest" title="pinterest">p</a><a href="#" class="linkedin" title="linkedin">in</a><a href="#" class="googleplus" title="googleplus">g</a><a href="#" class="twitter" title="twitter">t</a><a href="#" class="facebook" title="facebook">f</a></div></div>',
            enableHover: false,
            enableTracking: false,
            urlCurl: jQuery(this).data('curl'),
            buttons: {
                pinterest: {
                    media: jQuery(this).data('media')
                }
            },
            render: function(api, options) {
                jQuery(api.element).on('click', '.twitter', function() {
                    api.openPopup('twitter');
                    return false;
                });
                jQuery(api.element).on('click', '.facebook', function() {
                    api.openPopup('facebook');
                    return false;
                });
                jQuery(api.element).on('click', '.googleplus', function() {
                    api.openPopup('googlePlus');
                    return false;
                });
                jQuery(api.element).on('click', '.linkedin', function() {
                    api.openPopup('linkedin');
                    return false;
                });
                jQuery(api.element).on('click', '.pinterest', function() {
                    api.openPopup('pinterest');
                    return false;
                });
            }
        });
    });
});


function initFitvids()
{
    if (jQuery('.video_frame').length) {
        jQuery('.video_frame').fitVids();
    }
}

function is_safari()
{
    return navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1
}

function initYoutube(youtube_id, article) {

    if (youtube_id && !is_safari()) {
        var params = {
            allowFullScreen: 'true',
            allowScriptAccess: 'always',
            wmode: 'opaque'
        };
        var flashvars = {
            enablejsapi: '1',
            playerapiid: 'id' + youtube_id,
            id: 'id' + youtube_id
        };
        swfobject.embedSWF("http://www.youtube.com/v/" + youtube_id, "id" + youtube_id, "532", "300", "9.0.0", false, flashvars, params);

        if (typeof article != 'undefined') {

            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.text = " function pauseOtherExcept_id" + youtube_id.replace('-', '_') + "(state) { if(state == 1) { galleryVideo.pauseOther('id" + youtube_id + "'); } } ";
            article.append(script);
        }
    }
}

var galleryVideo = {
    vimeo: {},
    youtube: {},
    selfhosted: {},
    addVideoID: function(player, id) {
        if (player == 'youtube')
        {
            this.youtube[id] = id;
        }
        else if (player == 'vimeo')
        {
            this.vimeo[id] = id;
        }
        else
        {
            this.selfhosted[id] = id;
        }
    },
    play: function(player, id) {
        if (player == 'youtube')
        {
            this.playYoutube(id);
        }
        else if (player == 'vimeo')
        {
            this.playVimeo(id);
        }
        else
        {
            this.playSelfhosted(id);
        }
    },
    playVimeo: function(id) {
        var vp = $f(jQuery('#' + id).get(0));
        if (vp)
        {
            vp.api('play');
        }
    },
    playYoutube: function(id) {
        var ytplayer1 = document.getElementById('id' + id);
        if (ytplayer1)
        {
            ytplayer1.playVideo();
        }
    },
    playSelfhosted: function(id) {
        jQuery(id).jPlayer('play');
    },
    addAudio: function(player, id) {
        this.selfhosted[id] = id;
    },
    pauseOther: function(exceptID)
    {
        this.pauseYouTube(exceptID);
        this.pauseVimeo(exceptID);
        this.pauseSelfhosted(exceptID);
    },
    pauseYouTube: function(exceptID) {
        for (id in this.youtube)
        {
            var ytplayer1 = document.getElementById(id);
            if (ytplayer1)
            {
                var ytp1State = ytplayer1.getPlayerState();
                if (ytp1State == 1 && (ytplayer1.id != exceptID))
                {
                    ytplayer1.pauseVideo();
                }
            }
        }
    },
    pauseVimeo: function(exceptID) {
        for (id in this.vimeo)
        {
            if (id != exceptID)
            {
                var vp = $f(jQuery('#' + id).get(0));
                if (vp)
                {
                    vp.api('pause');
                }
            }
        }
    },
    pauseSelfhosted: function(exceptID) {
        for (id in this.selfhosted)
        {
            if (id != exceptID)
            {
                jQuery(id).jPlayer("pause"); // pause all players except this one.;
            }
        }
    }
};

var customPostColor = function($article) {

    jQuery($article).each(function() {
        if (jQuery(this).data('color')) {
            var color = jQuery(this).data('color');
            jQuery(this).find('.entry-content a:not(".ui-tabs-anchor"), .comment-entry a').css({
                'color': color
            });
            jQuery(this).find('.entry-content a:not(".ui-tabs-anchor"), .comment-entry a').on({
                mouseenter: function() {
                    jQuery(this).css({
                        'color': ''
                    });
                },
                mouseleave: function() {
                    jQuery(this).css({
                        'color': color
                    });
                }
            });
            
            /*

            jQuery(this).find('.qd_list li a').on({
                mouseenter: function() {
                    jQuery(this).css({
                        'color': color
                    });
                },
                mouseleave: function() {
                    jQuery(this).css({
                        'color': color
                    });
                }
            });

            jQuery(this).find('.zoom, .t_ico').css({
                'background-color': color
            });
            jQuery(this).find('li').on({
                mouseenter: function() {
                    jQuery(this).find('.mark').css({
                        'background-color': color
                    });
                },
                mouseleave: function() {
                    jQuery(this).find('.mark').css({
                        'background-color': ''
                    });
                }
            });
            */
            jQuery(this).find('.jp-play, .jp-pause, .jp-stop, .jp-video-play-icon, .qd_contact-submit .htg_button, #commentform #submit, a.qd_button_small, .password_submit').on({
                mouseenter: function() {
                    jQuery(this).css({
                        'background-color': color
                    });
                },
                mouseleave: function() {
                    jQuery(this).css({
                        'background-color': ''
                    });
                }
            });
            jQuery(this).find('.jp-pause').css({
                'background-color': color
            });

            // only for next prev link
            if (!color || color == '#')
            {
                color = jQuery(this).css('border-color');
            }
            jQuery(this).find('.blog_link a, .prev_post_link a, .next_post_link a').on({
                mouseenter: function() {
                    jQuery(this).css({
                        'background-color': color
                    });
                },
                mouseleave: function() {
                    jQuery(this).css({
                        'background-color': ''
                    });
                }
            });
        }
    });
}
